// LocalStorage 操作者
export class LocalStorageOperator<T> {

    //region # 字段及构造器

    // 键
    private _key:string

    /**
     * 有参构造器
     * @param key 键
     * */
    constructor(key:string) {
        this._key = key
    }

    //endregion

    //region # 方法

    /**
     * 设置
     * @param value 值
     * */
    public set(value:T):void {
        const valueStr:string = typeof value == 'object' ? value!.toString() : JSON.stringify(value)

        window.localStorage.setItem(this._key, valueStr)
    }

    /**
     * 获取
     * */
    public Get():T | null {
        const valueStr:string | null = window.localStorage.getItem(this._key)

        let result:T | null
        if (valueStr == null || valueStr == '') {
            result = null
        }
        else {
            result = typeof valueStr == 'object' ? valueStr : JSON.parse(valueStr)
        }

        return result
    }

    /**
     * 移除
     * */
    public remove() {
        window.localStorage.removeItem(this._key)
    }

    //endregion

}

/**
 * 清理全部
 * */
export function clear() {
    window.localStorage.clear()

}
